<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Bootstrap
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * @category   Zend
 * @package    Bootstrap
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    /**
     * Initialize start time (for logging)
     */
    protected function _initStartTime()
    {
        $startTime = microtime(true);
        Zend_Registry::set('startTime', $startTime);
    }

    /**
     * Initialize request (for getting of URL)
     */
    protected function _initRequest()
    {
        // Ensure front controller instance is present, and fetch it
        $this->bootstrap('FrontController');
        $front = $this->getResource('FrontController');

        // Initialize the request object
        $request = new Zend_Controller_Request_Http();

        // Add it to the front controller
        $front->setRequest($request);

        // Bootstrap will store this value in the 'request' key of its container
        return $request;
    }

    /**
     * Initialize config
     */
    protected function _initConfig()
    {
        $config = new Zend_Config($this->getOptions());
        Zend_Registry::set('config', $config);
    }

    /**
     * Initialize logging
     */
    protected function _initLogging()
    {
        // Initialize logger
        $logger = new Zend_Log();
        $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH
                        . '/../data/logs/app.log');
        $logger->addWriter($writer);

        // Detect log level
        if ('production' == $this->getEnvironment()) {
            $filter = new Zend_Log_Filter_Priority(
                            Zend_Log::INFO
            );
            $logger->addFilter($filter);
        }

        // Register logger
        Zend_Registry::set('log', $logger);
    }

    /**
     * Configure the default modules autoloading, here we first create
     * a new module autoloader specifiying the base path and namespace
     * for our default module. This will automatically add the default
     * resource types for us. We also add two custom resources for Services
     * and Model Resources.
     */
    protected function _initDefaultModuleAutoloader()
    {

        $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
                    'namespace' => 'Storefront',
                    'basePath' => APPLICATION_PATH . '/modules/storefront',
                ));
    }

    /**
     * Initialize doctype
     */
    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }

    /**
     * Initialize locale
     */
    protected function _initLocale()
    {
        // Get front controller with config 
        $frontController = Zend_Controller_Front::getInstance();
        $config = Zend_Registry::get('config');

        // Get languages from config
        $languages = $config->languages->toArray();
        $this->view->languages = array_keys($languages);

        // Detect language from URL
        $lang = substr($frontController->getRequest()->getRequestUri(), 1, 2);
        if (!$lang || !array_key_exists($lang, $languages)) {
            $lang = $config->defaultLanguage;
        }

        // Initialize and register locale
        $locale = new Zend_Locale($languages[$lang]);
        Zend_Registry::set('Zend_Locale', $locale);
    }

    /**
     * Initialize translation object
     */
    protected function _initTranslate()
    {
        // Initialize translation object
        $translate = new Zend_Translate('csv',
                        APPLICATION_PATH . '/../languages/',
                        null,
                        array('scan' => Zend_Translate::LOCALE_FILENAME, 'delimiter' => ','));
        // Register translation object
        Zend_Registry::set('Zend_Translate', $translate);
        Zend_Validate_Abstract::setDefaultTranslator($translate);
        Zend_Form::setDefaultTranslator($translate);
    }

    /**
     * Initialzie e-mail settings
     */
    protected function _initEmail()
    {
        $config = Zend_Registry::get('config');
        $emailConfig = array(
            'auth' => 'login',
            'username' => $config->email->username,
            'password' => $config->email->password,
            'ssl' => $config->email->protocol,
            'port' => $config->email->port
        );
        $mailTransport = new Zend_Mail_Transport_Smtp($config->email->server, $emailConfig);
        Zend_Mail::setDefaultTransport($mailTransport);
    }

    /**
     * Initialize routes
     */
    public function _initRoutes()
    {
        // Get router from conroller
        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();

        // Get language from locale
        $locale = Zend_Registry::get('Zend_Locale');
        $lang = $locale->getLanguage();

        // Default route
        $route = new Zend_Controller_Router_Route(
                        '*',
                        array('controller' => 'Index',
                            'action' => 'index',
                            'lang' => $lang));
        $router->addRoute('default', $route);

        // Storefront route for payment page
        $route = new Zend_Controller_Router_Route(
                        ':lang/payment',
                        array('controller' => 'Index',
                            'action' => 'payment',
                            'lang' => $lang,
                        )
        );
        $router->addRoute('payment', $route);

        // Storefront route for warranty page
        $route = new Zend_Controller_Router_Route(
                        ':lang/warranty',
                        array('controller' => 'Index',
                            'action' => 'warranty',
                            'lang' => $lang,
                        )
        );
        $router->addRoute('warranty', $route);

        // Storefront route for faq page
        $route = new Zend_Controller_Router_Route(
                        ':lang/faq',
                        array('controller' => 'Index',
                            'action' => 'faq',
                            'lang' => $lang,
                        )
        );
        $router->addRoute('faq', $route);

        // Storefront route for contacts page
        $route = new Zend_Controller_Router_Route(
                        ':lang/contacts',
                        array('controller' => 'Index',
                            'action' => 'contacts',
                            'lang' => $lang,
                        )
        );
        $router->addRoute('contacts', $route);


        // Storefront route for catalog page
        $route = new Zend_Controller_Router_Route(
                        ':lang/catalog/:id',
                        array('controller' => 'Catalog',
                            'action' => 'view',
                            'lang' => $lang,
                        )
        );
        $router->addRoute('catalog-view', $route);

        // Storefront route for product page
        $route = new Zend_Controller_Router_Route(
                        ':lang/product/:id',
                        array('controller' => 'Product',
                            'action' => 'view',
                            'lang' => $lang
                        )
        );
        $router->addRoute('product-view', $route);

        // Storefront route for search page
        $route = new Zend_Controller_Router_Route(
                        ':lang/search',
                        array('controller' => 'Search',
                            'action' => 'result',
                            'lang' => $lang
                        )
        );
        $router->addRoute('search-result', $route);

        // Storefront route for cart
        $route = new Zend_Controller_Router_Route(
                        ':lang/cart',
                        array('controller' => 'Cart',
                            'action' => 'index',
                            'lang' => $lang
                        )
        );
        $router->addRoute('cart-index', $route);

        // Storefront route for order
        $route = new Zend_Controller_Router_Route(
                        ':lang/order',
                        array('controller' => 'Order',
                            'action' => 'add',
                            'lang' => $lang
                        )
        );
        $router->addRoute('order-add', $route);

        // Admin route for start with language
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin',
                        array('controller' => 'Order',
                            'action' => 'index',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-lang-index', $route);

        // Admin route for login
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/login',
                        array('controller' => 'Auth',
                            'action' => 'login',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-login', $route);

        // Admin route for logout
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/logout',
                        array('controller' => 'Auth',
                            'action' => 'logout',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-logout', $route);

        // Admin route for orders
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/order/index',
                        array('controller' => 'Order',
                            'action' => 'index',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-orders', $route);

        // Admin route for order details
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/order/view/:id',
                        array('controller' => 'Order',
                            'action' => 'view',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-order-view', $route);

        // Admin route for editing of order
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/order/edit/:id',
                        array('controller' => 'Order',
                            'action' => 'edit',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-order-edit', $route);

        // Admin route for invoices
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/invoice/index',
                        array('controller' => 'Invoice',
                            'action' => 'index',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-invoices', $route);

        // Admin route for editing of invoice
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/invoice/edit/:order_id',
                        array('controller' => 'Invoice',
                            'action' => 'edit',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-invoice-edit', $route);

        // Admin route for shipments
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/shipment/index',
                        array('controller' => 'Shipment',
                            'action' => 'index',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-shipments', $route);

        // Admin route for editing of shipment
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/shipment/edit/:order_id',
                        array('controller' => 'Shipment',
                            'action' => 'edit',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-shipment-edit', $route);

        // Admin route for categories
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/category/index',
                        array('controller' => 'Category',
                            'action' => 'index',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-categories', $route);

        // Admin route for features
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/feature/index',
                        array('controller' => 'Feature',
                            'action' => 'index',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-features', $route);

        // Admin route for feature Groups
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/feature_group/index',
                        array('controller' => 'Group',
                            'action' => 'index',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-featureGroups', $route);

        // Admin route for products
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/product/index',
                        array('controller' => 'Product',
                            'action' => 'index',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-products', $route);

        // Admin route for price rules
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/price_rule/index',
                        array('controller' => 'Rule',
                            'action' => 'index',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-price-rules', $route);
        
        // Admin route for edit of price rule
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/price_rule/edit/:id',
                        array('controller' => 'Rule',
                            'action' => 'edit',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-price-rule-edit', $route);
        
        // Admin route for add of price rule
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/price_rule/add',
                        array('controller' => 'Rule',
                            'action' => 'edit',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-price-rule-add', $route);

        // Admin route for save of distributor
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/price_rule/save',
                        array('controller' => 'Rule',
                            'action' => 'save',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-price-rule-save', $route);        

        // Admin route for distributors
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/distributor/index',
                        array('controller' => 'Distributor',
                            'action' => 'index',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-distributors', $route);

        // Admin route for edit of distributor
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/distributor/edit/:id',
                        array('controller' => 'Distributor',
                            'action' => 'edit',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-distributor-edit', $route);

        // Admin route for add of distributor
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/distributor/add',
                        array('controller' => 'Distributor',
                            'action' => 'edit',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-distributor-add', $route);

        // Admin route for save of distributor
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/distributor/save',
                        array('controller' => 'Distributor',
                            'action' => 'save',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-distributor-save', $route);

        // Admin route for pricelists
        $route = new Zend_Controller_Router_Route(
                        ':lang/admin/distributor_price/index',
                        array('controller' => 'Price',
                            'action' => 'index',
                            'module' => 'admin',
                            'lang' => $lang
                        )
        );
        $router->addRoute('admin-pricelists', $route);

        // Set initialized router
        $frontController->setRouter($router);
    }

}

